package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.WendaAnswer;
import cn.wolfcode.luowowo.article.domain.WendaAnswerContent;
import cn.wolfcode.luowowo.article.mapper.WendaAnswerContentMapper;
import cn.wolfcode.luowowo.article.mapper.WendaAnswerMapper;
import cn.wolfcode.luowowo.article.query.WendaAnswerQuery;
import cn.wolfcode.luowowo.article.service.IWendaAnswerService;
import cn.wolfcode.luowowo.common.util.HtmlUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class WendaAnswerServiceImpl implements IWendaAnswerService {
    @Autowired
    private WendaAnswerMapper wendaAnswerMapper;
    @Autowired
    private WendaAnswerContentMapper wendaAnswerContentMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return wendaAnswerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(WendaAnswer wendaAnswer) {
        return wendaAnswerMapper.insert(wendaAnswer);
    }

    @Override
    public WendaAnswer selectByPrimaryKey(Long id) {
        return wendaAnswerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<WendaAnswer> selectAll() {
        return wendaAnswerMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(WendaAnswer wendaAnswer) {
        return wendaAnswerMapper.updateByPrimaryKey(wendaAnswer);
    }

    @Override
    public Long saveOrUpdate(WendaAnswer wendaAnswer) {
        //获取问答内容
        WendaAnswerContent wendaAnswerContent = wendaAnswer.getWendaAnswerContent();
        //判断是否存在内容对象
        if (wendaAnswerContent != null){
            //判断是否存在内容
            if (!StringUtils.isEmpty(wendaAnswerContent.getContent())){
                //截取第一张图片
                String lastImage = HtmlUtils.subLastImage(wendaAnswerContent.getContent());
                //判断该图片是否为空,不为空则设置到answer里面
                if (!StringUtils.isEmpty(lastImage)){
                    wendaAnswer.setCoverUrl(lastImage);
                }
                //截取answer的内容
                String htmlText = HtmlUtils.subHtmlText(wendaAnswerContent.getContent());
                //判断是否存在内容
                if (!StringUtils.isEmpty(htmlText)){
                    //保存答案的摘要
                    if (htmlText.length()>200){
                        wendaAnswer.setSummary(htmlText.substring(0,200));
                    }else {
                        wendaAnswer.setSummary(htmlText);
                    }
                }
            }
        }
        //判断是新增还是
        if(wendaAnswer.getId() == null){
            //创建时间
            wendaAnswer.setCreateTime(new Date());
            wendaAnswerMapper.insert(wendaAnswer);

            // 保存回答内容数据
            wendaAnswerContent.setId(wendaAnswer.getId());
            wendaAnswerContentMapper.insert(wendaAnswerContent);

        }else{
            wendaAnswerMapper.updateByPrimaryKey(wendaAnswer);

            wendaAnswerContent.setId(wendaAnswer.getId());
            wendaAnswerContentMapper.updateByPrimaryKey(wendaAnswerContent);
        }

        return wendaAnswer.getId();
    }

    @Override
    public PageInfo query(WendaAnswerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(wendaAnswerMapper.selectForList(qo));
    }

    @Override
    public WendaAnswerContent getContent(Long id) {
        return wendaAnswerContentMapper.selectById(id);
    }
}
